/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.component.cells;

import cn.ac.nercis.pes.community.model.vo.TableDataModel;
import javafx.scene.control.Pagination;
import javafx.scene.control.TableCell;

import java.util.Objects;

/**
 * 表序号列
 * @param <T>
 * @author zpy
 * @version 1.0.0
 */
public class NumberTableCell<T> extends TableCell<T,String> {
    private final Pagination pagination;
    private final Integer startIndex;

    public NumberTableCell(Pagination pagination,Integer startIndex) {
        this.pagination = pagination;
        this.startIndex = startIndex;
    }

    public NumberTableCell(Pagination pagination) {
        this(pagination,0);
    }

    public NumberTableCell(Integer startIndex) {
        this(null,startIndex);
    }

    public NumberTableCell(){
        this(0);
    }

    @Override
    protected void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);
        this.setText(null);
        this.setGraphic(null);
        int startIndex = this.startIndex;
        if(Objects.nonNull(pagination)){
            startIndex = pagination.getCurrentPageIndex() * TableDataModel.LIMIT;
        }
        if(!empty){
            int rowIndex = startIndex + this.getIndex() + 1;
            this.setText(String.valueOf(rowIndex));
        }
    }
}